package com.code.generate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.code.generate.enums.ControlType;
import com.code.generate.enums.FieldType;
import javafx.scene.input.DataFormat;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.code.generate.enums.ControlType.*;

/**
 * @author simple
 * @ClassName: CoastCodeGenerator
 * @Description: TODO(describe this class function)
 * @date Jul 6, 2016 10:43:02 AM
 */
public class CoastCodeGenerator {

    public void setVariable(Map<String, String> variable, List<Field> fields) {
        variable.put("@{domain_field_list}", getFieldDeclaredStr(fields));
        //查询的DTO属性
        variable.put("@{domain_query_dto_field_list}", getFieldQueryDtoDeclaredStr(fields));
        //保存的DTO属性
        variable.put("@{domain_save_dto_field_list}", getFieldSaveDtoDeclaredStr(fields));
        //更新的DTO属性
        variable.put("@{domain_update_dto_field_list}", getFieldUpdateDtoDeclaredStr(fields));
        //ALL DTO属性
        variable.put("@{domain_dto_field_list}", getFieldDtoDeclaredStr(fields));
        variable.put("@{domain_vo_field_list}", getFieldVODeclaredStr(fields));
        variable.put("@{domain_field_list_set_get}", getFieldGetSetFunctionStr(fields));
        variable.put("@{to_string}", getToString(fields));
        variable.put("@{resultMap_info}", getResultMapInfo(fields));
        variable.put("@{Base_Column_List}", getBaseColumnList(fields));
        variable.put("@{column_insert_values_list}", getInsertColumnValuesList(fields));
        variable.put("@{insert_column_selective}", getInsertColumnSelective(fields));
        variable.put("@{insert_values_column_selective}", getInsertValuesColumnSelective(fields));
        variable.put("@{update_set_column_selective}", getUpdateSetColumnSelective(fields));
        variable.put("@{dynamic_condition}", getDynamicCondition(fields));
        variable.put("@{insert_batch_values}", getInsertBatchValues(fields));
        variable.put("@{domain_field_import_list}", getPojoImportFieldType(fields));
        variable.put("@{data}", new SimpleDateFormat("yyyy年MM月dd日 HH时mm分").format(new Date()));

        /*
        variable.put("@{js_init_column_list}", getJsInitColumnList(fields));//JS 初始化列字段列表 ,显示列表: true
        variable.put("@{html_search_column}", getHtmlSearchColumn(fields));//HTML 查询条件 ,查询条件: true
        variable.put("@{html_modal_edit}", getHtmlModalEdit(fields));//HTML 获取模式窗口编辑元素whetherEdit:true
        */


        /**
         * vue
         * //表格列表元素
         * @{vue_table_column_html}
         *
         * //编辑内容
         * @{vue_form_item_html}
         *
         * //下拉框初始化JS
         * @{vue_options_init_js}
         *
         * //下拉框导入到VUE
         * @{vue_options_init_js_import}
         *
         * //表单from元素声明
         * @{vue_form_item_js}
         *
         * //表单from规则声明
         * @{vue_form_rules_js}
         *
         * //过滤
         * @{vue_options_filters_js}
         */
        //表格列表元素
        variable.put("@{vue_table_column_html}", getVueTableColumnHtml(fields));
        variable.put("@{vue_form_item_html}", getVueFormItemHtml(fields));
        variable.put("@{vue_options_init_js}", getVueOptionsInitJs(fields));
        variable.put("@{vue_options_init_js_import}", getVueOptionsInitJsImport(fields));
        variable.put("@{vue_options_filters_js}", getVueOptionsFiltersJs(fields));
        variable.put("@{vue_form_item_js}", getVueFormItemJs(fields));
        variable.put("@{vue_form_rules_js}", getVueFormRulesJs(fields));
    }

    private String getVueOptionsFiltersJs(List<Field> fields) {

        StringBuffer htmlBuff = new StringBuffer();
        fields.forEach((field) -> {

            final ControlType inputType = field.getInputType();

            if(inputType.equals(ControlType.SELECT) || inputType.equals(ControlType.SELECT_CHECKBOX)){

                String temp = "%sFilter(status) {\n" +
                        "\t\t\treturn %sKeyValue[status];\n" +
                        "\t\t},";

                htmlBuff.append(String.format(temp , field.getFieldName(),field.getFieldName()));
            }
        });


        return htmlBuff.toString();
    }


    private String getVueFormRulesJs(List<Field> fields) {

        StringBuffer buffer = new StringBuffer();
        fields.forEach((field) -> {

            if (!field.isWhetherEdit()) {
                return;
            }

            final String fieldName = field.getFieldName();
            final String fieldCName = field.getFieldCName();
            final ControlType inputType = field.getInputType();
            final int length = field.getLength();
            final boolean whetherRequest = field.isWhetherRequest();

            buffer.append(", "+fieldName+" : [");
            if(whetherRequest) {

                String message = "请输入" + fieldCName;
                if(inputType.equals(ControlType.SELECT) || inputType.equals(ControlType.SELECT_CHECKBOX) || inputType.equals(ControlType.RADIO)){
                    message = "请选择" + fieldCName;
                }

                buffer.append(String.format("{\n" +
                        "\t\t\t\t\t\trequired: true,\n" +
                        "\t\t\t\t\t\tmessage: '%s',\n" +
                        "\t\t\t\t\t\ttrigger: 'blur'\n" +
                        "\t\t\t\t\t}," , message));
            }

            if(inputType.equals(ControlType.TEXT) || inputType.equals(ControlType.TEXTAREA)) {

                buffer.append(String.format("{\n" +
                        "\t\t\t\t\t\tmax: %s,\n" +
                        "\t\t\t\t\t\tmessage: '长度在 %s 个字符',\n" +
                        "\t\t\t\t\t\ttrigger: 'blur'\n" +
                        "\t\t\t\t\t}" , length , length));
            }

            buffer.append("]");
        });

        return "{" + buffer.toString().substring(1) + "}";
    }

    private String getVueFormItemJs(List<Field> fields) {

        StringBuffer buffer = new StringBuffer();
        fields.forEach((field) -> {

            if (!field.isWhetherEdit() && field.isWhetherPrimaryKey()) {
                return;
            }

            final ControlType inputType = field.getInputType();
            switch (inputType){
                case SELECT_CHECKBOX:{
                    buffer.append(String.format(",%s : []" , field.getFieldName()));
                    break;
                }
                default:{
                    buffer.append(String.format(",%s : ''" , field.getFieldName()));
                }
            }

        });
        return "{" + buffer.toString().substring(1) + "}";
    }

    private String getVueOptionsInitJsImport(List<Field> fields) {

        StringBuffer htmlBuff = new StringBuffer();
        fields.forEach((field) -> {

            final ControlType inputType = field.getInputType();

            if(inputType.equals(ControlType.SELECT) || inputType.equals(ControlType.SELECT_CHECKBOX)){
                String temp = "%sOptions,";
                htmlBuff.append(String.format(temp , field.getFieldName()));
            }
        });


        return htmlBuff.toString();
    }

    private String getVueOptionsInitJs(List<Field> fields) {

        StringBuffer htmlBuff = new StringBuffer();
        fields.forEach((field) -> {

            final ControlType inputType = field.getInputType();

            if(inputType.equals(ControlType.SELECT) || inputType.equals(ControlType.SELECT_CHECKBOX)){

                String temp = "const %sOptions = [\n" +
                        "\t{\n" +
                        "\t\tkey: 'Y',\n" +
                        "\t\tvalue: '有效'\n" +
                        "\t},\n" +
                        "\t{\n" +
                        "\t\tkey: 'N',\n" +
                        "\t\tvalue: '禁用'\n" +
                        "\t}\n" +
                        "];\n";

                htmlBuff.append(String.format(temp , field.getFieldName()));

                temp = "const %sKeyValue = %sOptions.reduce((acc, cur) => {\n" +
                        "\tacc[cur.key] = cur.value;\n" +
                        "\treturn acc;\n" +
                        "}, {});";

                htmlBuff.append(String.format(temp , field.getFieldName(),field.getFieldName()));
            }
        });


        return htmlBuff.toString();
    }


    private String getVueFormItemHtml(List<Field> fields) {

        StringBuffer htmlBuff = new StringBuffer();
        fields.forEach((field) -> {

            if(!field.isWhetherEdit()){
                return;
            }

            final ControlType inputType = field.getInputType();

            switch (inputType) {
                case TEXT:{
                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\t\t\t\t\t<el-input v-model=\"form.%s\" placeholder=\"请输入%s\" ></el-input>\n" +
                            "\t\t\t\t</el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName() , field.getFieldCName()));
                    break;
                }

                case TEXTAREA:{

                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\t\t\t\t\t<el-input type=\"textarea\" v-model=\"form.%s\" :autosize=\"{ minRows: 2, maxRows: 4}\"></el-input>\n" +
                            "\t\t\t\t</el-form-item>";

                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName()));
                    break;

                }

                case SELECT:{

                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\t\t\t\t\t<el-select v-model=\"form.%s\" placeholder=\"请选择\" style=\"width:100\">\n" +
                            "\t\t\t\t\t\t<el-option v-for=\"item in %sOptions\" :key=\"item.key\" :label=\"item.value\" :value=\"item.key\">\n" +
                            "\t\t\t\t\t\t</el-option>\n" +
                            "\t\t\t\t\t</el-select>\n" +
                            "\t\t\t\t</el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName() , field.getFieldName()));
                    break;
                }

                case SELECT_CHECKBOX:{
                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\t\t\t\t\t<el-select v-model=\"form.%s\" placeholder=\"请选择\" multiple style=\"width:100\">\n" +
                            "\t\t\t\t\t\t<el-option v-for=\"item in %sOptions\" :key=\"item.key\" :label=\"item.value\" :value=\"item.key\">\n" +
                            "\t\t\t\t\t\t</el-option>\n" +
                            "\t\t\t\t\t</el-select>\n" +
                            "\t\t\t\t</el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName() , field.getFieldName()));
                    break;
                }

                case DATE:{

                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\n" +
                            "\t\t\t\t\t<el-date-picker v-model=\"from.%s\" type=\"date\" placeholder=\"选择日期\">\n" +
                            "\t\t\t\t\t</el-date-picker>\n" +
                            "\t\t\t\t</el-form-item>";

                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName()));
                    break;

                }

                case DATE_TIME:{
                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\n" +
                            "\t\t\t\t\t<el-date-picker v-model=\"from.%s\" type=\"datetime\" placeholder=\"选择日期时间\">\n" +
                            "\t\t\t\t\t</el-date-picker>\n" +
                            "\t\t\t\t</el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName()));
                    break;
                }

                case TIME:{
                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\n" +
                            "\t\t\t\t\t<el-time-select v-model=\"from.%s\" placeholder=\"选择日期时间\">\n" +
                            "\t\t\t\t\t</el-time-select>\n" +
                            "\t\t\t\t</el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName()));
                    break;
                }

                case RADIO:{
                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "<el-radio-group v-model=\"form.%s\">\n" +
                            "      <el-radio label=\"1\">redio-1</el-radio>\n" +
                            "      <el-radio label=\"2\">redio-2</el-radio>\n" +
                            "    </el-radio-group>" +
                            "    </el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName()));
                    break;
                }

                default:{
                    String temp = "<el-form-item label=\"%s\" prop=\"%s\">\n" +
                            "\t\t\t\t\t<el-input v-model=\"form.%s\" placeholder=\"请输入%s\" ></el-input>\n" +
                            "\t\t\t\t</el-form-item>";
                    htmlBuff.append(String.format(temp, field.getFieldCName() , field.getFieldName() , field.getFieldName() , field.getFieldCName()));
                    break;
                }



            }


        });



        return htmlBuff.toString();
    }


    private String getVueTableColumnHtml(List<Field> fields) {

        String textTemp = "<el-table-column align=\"%s\" label=\"%s\">\n" +
                "\t\t\t\t<template slot-scope=\"scope\">\n" +
                "\t\t\t\t\t\t%s\n" +
                "\t\t\t\t</template>\n" +
                "\t\t\t</el-table-column>";

        String span = "<span>%s</span>";

        String tag = "<el-tag>%s</el-tag>";


        String textareaTemp = "<el-table-column align=\"%s\" label=\"%s\" :show-overflow-tooltip=\"true\">\n" +
                "\t\t\t\t<template slot-scope=\"scope\">\n" +
                "\t\t\t\t\t\t%s\n" +
                "\t\t\t\t</template>\n" +
                "\t\t\t</el-table-column>";


        StringBuffer htmlBuff = new StringBuffer();

        //遍历元素
        fields.forEach((field) -> {

            //是否在列表中显示
            if (!field.isWhetherList()) {
                return;
            }

            final ControlType inputType = field.getInputType();

            String align = "left";
            if(field.getDataType().equals(FieldType.INTEGER) || field.getDataType().equals(FieldType.DOUBLE) || field.getDataType().equals(FieldType.LONG)){
                align = "right";
            }else if(field.getDataType().equals(FieldType.DATE) || field.getDataType().equals(FieldType.DATE_TIME)){
                align = "center";
            }

            switch (inputType) {

                case TEXT: {
                    final String format = String.format(textTemp, align, field.getFieldCName(), String.format(span, "{{scope.row." + field.getFieldName() + "}}"));
                    htmlBuff.append(format);
                    break;
                }
                case DATE_TIME: {
                    final String format = String.format(textTemp, align, field.getFieldCName(), String.format(span, "{{scope.row." + field.getFieldName() + " | parseTime('{y}-{m}-{d}')}}"));
                    htmlBuff.append(format);
                    break;
                }
                case DATE: {
                    final String format = String.format(textTemp, align, field.getFieldCName(), String.format(span, "{{scope.row." + field.getFieldName() + " | parseTime('{y}-{m}-{d}')}}"));
                    htmlBuff.append(format);
                    break;
                }
                case SELECT: {
                    final String format = String.format(textTemp, "center", field.getFieldCName(), String.format(tag, "{{scope.row." + field.getFieldName() + " | " + field.getFieldName() + "Filter}}"));
                    htmlBuff.append(format);
                    break;
                }
                case SELECT_CHECKBOX: {
                    final String format = String.format(textTemp, "center", field.getFieldCName(), String.format(tag, "{{scope.row." + field.getFieldName() + " | " + field.getFieldName() + "Filter}}"));
                    htmlBuff.append(format);
                    break;
                }
                case TEXTAREA: {
                    final String format = String.format(textareaTemp, align, field.getFieldCName(), String.format(span, "{{scope.row." + field.getFieldName() + "}}"));
                    htmlBuff.append(format);
                    break;
                }
                default: {
                    final String format = String.format(textTemp, align , field.getFieldCName(), String.format(span, "{{scope.row." + field.getFieldName() + "}}"));
                    htmlBuff.append(format);
                    break;
                }

            }


        });


        return htmlBuff.toString();
    }

    /**
     * 获取模式窗口编辑
     *
     * @param fields
     * @return
     */
    private String getHtmlModalEdit(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        sb.append("<form role=\"form\" id=\"edit-form\" method=\"post\" class=\"validate\">");
        for (Field field : fields) {
            boolean whetherEdit = field.isWhetherEdit();//是否可编辑
            boolean whetherPrimaryKey = field.isWhetherPrimaryKey();//是否主键
            boolean whetherRequest = field.isWhetherRequest();//是否必填

            String dataType = field.getDataType();

            int length = 0;
            if (dataType.equals("String")) {//String 数据库长度 / 2
                length = (field.getLength() / 2);//长度
            } else if (dataType.equals("int") || dataType.equals("Integer") || dataType.equals("Long")) {
                length = field.getLength();
            }


            String fieldCName = field.getFieldCName();//中文名
            String fieldName = field.getFieldName();//字段名

            String inputType = field.getInputType() == null ? TEXT.getValue() : field.getInputType().getValue();

            if (whetherPrimaryKey || !whetherEdit) {
                sb.append(String.format("<input type=\"text\" name=\"%s\" id=\"%s\" class=\"hidden-input\"/>", fieldName, fieldName));
                continue;
            }

            String requiredHTML = "";
            if (whetherRequest) {
                requiredHTML = "data-validate=\"required\"";
            }

            String maxLengthHTML = "";
            if (length > 0) {
                maxLengthHTML = String.format("maxlength=\"%s\"", length);
            }


            sb.append("<div class=\"form-group\">");
            sb.append(String.format("<label class=\"control-label\">%s</label>", fieldCName));

            if (inputType.equals(TEXT.getValue())) {//文本编辑
                sb.append(String.format("<input type=\"text\" class=\"form-control\" name=\"%s\" placeholder=\"请输入%s\" %s %s/>", fieldName, fieldCName, maxLengthHTML, requiredHTML));
            } else if (inputType.equals(TEXTAREA.getValue())) {//文本域
                sb.append(String.format("<textarea class=\"form-control autogrow\" placeholder=\"\" name=\"%s\" rows=\"5\" %s></textarea>", fieldName, maxLengthHTML));
            } else if (inputType.equals(SELECT.getValue())) {//下拉框

                String required = "";
                if (whetherRequest) {
                    required = "required";
                }

                sb.append(String.format("<select class=\"form-control select2\" name=\"%s\" placeholder=\"请选择%s\" %s>", fieldName, fieldCName, required));

                if (fieldName.equals("status")) {
                    sb.append("<option value=\"Y\">有效</option>");
                    sb.append("<option value=\"N\">无效</option>");
                }
                sb.append("</select>");
            }
            sb.append("</div>");

        }
        sb.append("</form>");
        return sb.toString();
    }


    /**
     * 获取页面搜索字段
     *
     * @param fields
     * @return
     */
    private String getHtmlSearchColumn(List<Field> fields) {
        StringBuffer sb = new StringBuffer();

        //获取查询字段列表
        List<Field> queryFields = new ArrayList<>();
        for (Field field : fields) {
            if (!field.isWhetherQuery()) {
                continue;
            }
            queryFields.add(field);
        }


        //补充列, 如果有10个字段, 补充空的列
        int queryFieldsSize = queryFields.size();
        while (queryFieldsSize % 3 != 0) {
            Field field = new Field();
            field.setFieldCName("");
            field.setFieldName("");
            queryFields.add(field);
            queryFieldsSize = queryFields.size();
        }

        int i = 1;
        for (Field field : queryFields) {

            if (i % 3 == 1) {//判读是否为一行的开始
                sb.append("<div class=\"row\">");
            }

            sb.append("<div class=\"col-xs-4\">");
            sb.append("<div class=\"form-group\">");
            sb.append(String.format("<label class=\"control-label\">%s</label>", field.getFieldCName()));
            sb.append(String.format("<input type=\"text\" class=\"form-control\" name=\"%s\">", field.getFieldName()));
            sb.append("</div>");

            sb.append("</div>");

            if (i % 3 == 0) {//判断为一行的结束
                sb.append("</div>");
            }
            i++;
        }

        return sb.toString();
    }

    /**
     * 获取JS出次数显示列
     *
     * @param fields
     * @return
     */
    private String getJsInitColumnList(List<Field> fields) {
        JSONArray array = new JSONArray();

        JSONObject json = new JSONObject();
        json.put("checkbox", true);
        array.add(json);

        for (Field field : fields) {
            if (!field.isWhetherList()) {
                continue;//是否为显示的列表
            }

            json = new JSONObject();
            json.put("field", field.getFieldName());
            json.put("title", field.getFieldCName());
            json.put("width", 120);

            if (field.getTableField().equalsIgnoreCase("status")) {
                json.put("width", 50);
                json.put("align", "center");
                json.put("formatter", "@@formatter.status@@");
            }

            if (field.getTableField().endsWith("time")) {
                json.put("width", 100);
                json.put("align", "center");
                json.put("formatter", "@@formatter.date2@@");
            }
            array.add(json);
        }

        String function =
                "@@function (value, row, index)" +
                        "{" +
                        "var edit = '<a href=\"#\" class=\"btn btn-white btn-xs\" onclick=\"ButtonInit().editInfo(\\'' + row.uuid + '\\')\"><i class=\"linecons-pencil\"></i>编辑</a>';" +
                        "return edit;" +
                        "}@@";
        json = new JSONObject();
        json.put("title", "操作");
        json.put("width", 130);
        json.put("formatter", function);
        array.add(json);


        String str = array.toJSONString();
        str = str.replaceAll("\"@@", "");
        str = str.replaceAll("@@\"", "");

        return str;
    }

    private String getPojoImportFieldType(List<Field> fields) {
        StringBuilder sb = new StringBuilder();
        for (Field field : fields) {
            String s = FieldType.getImportString(String.valueOf(field.getFieldType()));
            if (s != null && s.length() != 0) {
                sb.append(s).append("\n");
            }
        }
        return sb.toString();
    }

    private String getInsertBatchValues(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        int size = fields.size();
        for (Field field : fields) {
            sb.append(String.format("\t\t\t#{item.%s,jdbcType=%s}", field.getFieldName(), field.getDataType()));
            if (i + 1 != size) {
                sb.append(",\n");
            }
            i++;
        }
        return sb.toString();
    }

    private String getToString(List<Field> fields) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Field field : fields) {
            sb.append(String.format("%s='%s',", field.getFieldName(), "%s"));
        }
        sb.append("]\",");
        int size = fields.size();
        int i = 0;
        for (Field field : fields) {
            i++;
            sb.append(String.format("%s", field.getFieldName()));
            if (i != size) {
                sb.append(",");
            }
        }
        sb.append(");");
        System.out.println("to string : " + sb.toString());
        return sb.toString();
    }

    /**
     * 获取字段的声明
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldDeclaredStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            sb.append(String.format("\t/**%n\t* %s : %s %n\t*/%n", field.getFieldName(), field.getRemark()));
            if (field.isWhetherPrimaryKey()) {
                sb.append(String.format("\t@TableId(value=\"%s\")%n", field.getTableField()));
            } else {
                sb.append(String.format("\t@TableField(\"%s\")%n", field.getTableField()));
            }
            /*
            if (field.whetherRequest) {
                sb.append(String.format("\t@NotNull(message = \"%s.must.notnull\")%n", field.getFieldName()));
            }
            if (field.length > 0) {
                sb.append(String.format("\t@Size(max = %d, message = \"%s.size.beyond.error\")%n", field.getLength(), field.getFieldName()));
            }
            */
            sb.append(String.format("\tprivate %s %s;%n%n", field.getFieldType(), field.getFieldName()));
        }
        return sb.toString();
    }


    /**
     * 获取字段的声明
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldUpdateDtoDeclaredStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            if(!field.isWhetherEdit() && !field.isWhetherPrimaryKey()){
                continue;
            }
            sb.append(String.format("\t/**%n\t* %s : %s %n\t*/%n", field.getFieldName(), field.getRemark()));
            if (field.whetherRequest || field.isWhetherPrimaryKey()) {
                sb.append(String.format("\t@NotNull(message = \"%s不允许为空\")%n", field.getRemark()));
                sb.append(String.format("\t@ApiModelProperty(required= true,value = \"%s\")%n", field.getRemark()));
            } else {
                sb.append(String.format("\t@ApiModelProperty(value = \"%s\")%n", field.getRemark()));
            }
            if (field.length > 0) {
                sb.append(String.format("\t@Size(max = %d, message = \"%s超出长度限制\")%n", field.getLength(), field.getRemark()));
            }
            sb.append(String.format("\tprivate %s %s;%n%n", field.getFieldType(), field.getFieldName()));
        }
        return sb.toString();
    }


    /**
     * 获取字段的声明
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldSaveDtoDeclaredStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            if(!field.isWhetherEdit()){
                continue;
            }
            sb.append(String.format("\t/**%n\t* %s : %s %n\t*/%n", field.getFieldName(), field.getRemark()));
            if (field.whetherRequest) {
                sb.append(String.format("\t@NotNull(message = \"%s不允许为空\")%n", field.getRemark()));
                sb.append(String.format("\t@ApiModelProperty(required= true,value = \"%s\")%n", field.getRemark()));
            } else {
                sb.append(String.format("\t@ApiModelProperty(value = \"%s\")%n", field.getRemark()));
            }
            if (field.length > 0) {
                sb.append(String.format("\t@Size(max = %d, message = \"%s超出长度限制\")%n", field.getLength(), field.getRemark()));
            }
            sb.append(String.format("\tprivate %s %s;%n%n", field.getFieldType(), field.getFieldName()));
        }
        return sb.toString();
    }


    /**
     * 获取字段的声明
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldDtoDeclaredStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            sb.append(String.format("\t/**%n\t* %s : %s %n\t*/%n", field.getFieldName(), field.getRemark()));
            if (field.whetherRequest) {
                sb.append(String.format("\t@NotNull(message = \"%s不允许为空\")%n", field.getRemark()));
                sb.append(String.format("\t@ApiModelProperty(required= true,value = \"%s\")%n", field.getRemark()));
            } else {
                sb.append(String.format("\t@ApiModelProperty(value = \"%s\")%n", field.getRemark()));
            }
            if (field.length > 0) {
                sb.append(String.format("\t@Size(max = %d, message = \"%s超出长度限制\")%n", field.getLength(), field.getRemark()));
            }
            sb.append(String.format("\tprivate %s %s;%n%n", field.getFieldType(), field.getFieldName()));
        }
        return sb.toString();
    }


    /**
     * 获取字段的声明
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldQueryDtoDeclaredStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            if(!field.isWhetherQuery()){
                continue;
            }
            sb.append(String.format("\t/**%n\t* %s : %s %n\t*/%n", field.getFieldName(), field.getRemark()));
            sb.append(String.format("\t@ApiModelProperty(value = \"%s\")%n", field.getRemark()));
            sb.append(String.format("\tprivate %s %s;%n%n", field.getFieldType(), field.getFieldName()));
        }
        return sb.toString();
    }


    /**
     * 获取字段的声明
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldVODeclaredStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            sb.append(String.format("\t/**%n\t* %s : %s %n\t*/%n", field.getFieldName(), field.getRemark()));

            if(field.getDataType().equals(FieldType.DATE)){
                sb.append("@JsonFormat(timezone = \"GMT+8\", pattern = \"yyyy-MM-dd\")");
            }else if(field.getDataType().equals(FieldType.DATE_TIME)){
                sb.append("@JsonFormat(timezone = \"GMT+8\", pattern = \"yyyy-MM-dd HH:mm:ss\")");
            }
            sb.append(String.format("\t@ApiModelProperty(value = \"%s\")%n", field.getRemark()));
            sb.append(String.format("\tprivate %s %s;%n%n", field.getFieldType(), field.getFieldName()));
        }
        return sb.toString();
    }

    /**
     * 获取字段的getset函数
     *
     * @param fields
     * @return
     * @description: <方法描述>
     */
    private String getFieldGetSetFunctionStr(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {

            sb.append(String.format("\tpublic %s get%s(){%n\t\treturn this.%s;%n\t}%n%n",
                    field.getFieldType(),
                    CommonUtils.changeFirstUpper(field.getFieldName()),
                    field.getFieldName()));
            // setter
            sb.append(String.format("\tpublic void set%s(%s %s){%n\t\tthis.%s=%s;%n\t}%n%n",
                    CommonUtils.changeFirstUpper(field.getFieldName()),
                    field.getFieldType(),
                    field.getFieldName(),
                    field.getFieldName(),
                    field.getFieldName()));
        }
        return sb.toString();
    }

    /**
     * @param @param fields @param @return @return String @throws
     * @Title: getDynamicCondition @Description: TODO @Algorithm: Algorithm
     * Explanation<br>
     */
    private String getDynamicCondition(List<Field> fields) {
        StringBuffer sb = new StringBuffer();

        int i = 0;
        for (Field field : fields) {
            if (i == 0) {
                i++;
                continue;
            }
            sb.append(String.format("\t\t<if test=\"%s != null\">%n", field.getFieldName()));
            sb.append(String.format("\t\t\t AND %s = #{%s,jdbcType=%s}%n", field.getTableField(), field.getFieldName(), field.getDataType()));
            sb.append("\t\t</if>\n");
        }
        return sb.toString();
    }

    /**
     * @param @param fields @param @return @return String @throws
     * @Title: getUpdateSetColumnSelective @Description: @{update_set_column_selective} @Algorithm:
     * Algorithm Explanation<br>
     */
    private String getUpdateSetColumnSelective(List<Field> fields) {
        StringBuffer sb = new StringBuffer();

        int i = 0;
        int size = fields.size();
        for (Field field : fields) {
            if (i == 0) {
                i++;
                continue;
            }
            sb.append(String.format("\t\t<if test=\"%s != null\">%n", field.getFieldName()));
            sb.append(String.format("\t\t\t%s = #{%s,jdbcType=%s}", field.getTableField(), field.getFieldName(), field.getDataType()));
            if (i + 1 != size) {
                sb.append(",\n");
            }
            sb.append("\t\t</if>\n");
        }
        return sb.toString();
    }

    /**
     * @param @param fields @param @return @return String @throws
     * @Title: getInsertValuesColumnSelective @Description: @{insert_values_column_selective} @Algorithm:
     * Algorithm Explanation<br>
     */
    private String getInsertValuesColumnSelective(List<Field> fields) {
        StringBuffer sb = new StringBuffer();

        for (Field field : fields) {
            sb.append("			<if test=\"");
            sb.append(field.getFieldName());
            sb.append(" != null\">\n");
            sb.append("				#{" + field.getFieldName() + ",jdbcType=").append(field.getDataType()).append("},\n");
            sb.append("			</if>\n");
        }
        return sb.toString();
    }

    /**
     * @param @param fields @param @return @return String @throws
     * @Title: getInsertColumnSelective @Description: @{insert_column_selective} @Algorithm:
     * Algorithm Explanation<br>
     */
    private String getInsertColumnSelective(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        for (Field field : fields) {
            sb.append("			<if test=\"");
            sb.append(field.getFieldName());
            sb.append(" != null\">\n");
            sb.append("				").append(field.getTableField());
            sb.append(",\n");
            sb.append("			</if>\n");
        }
        return sb.toString();
    }

    /**
     * @Title: getColumnInsertValuesList
     * @Description: @{column_insert_values_list} 单条插入语句中的值字符串（values(...)）
     */
    private String getInsertColumnValuesList(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        int size = fields.size();
        for (Field field : fields) {

            if (field.getTableField().equalsIgnoreCase("RecordChangedTime")) {
                sb.append("current_timestamp");
            } else {
                sb.append(String.format("\t\t\t#{%s,jdbcType=%s}", field.getFieldName(), field.getDataType()));
            }
            if (i + 1 != size) {
                sb.append(",\n");
            }
            i++;
        }
        return sb.toString();
    }

    /**
     * @param @param fields @param @return @return String @throws
     * @Title: getBaseColumnList @Description: @{Base_Column_List} @Algorithm:
     * Algorithm Explanation<br>
     */
    private String getBaseColumnList(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        for (Field field : fields) {
            sb.append(field.getTableField());
            if (i + 1 != fields.size()) {
                sb.append(",");
            }
            i++;
        }
        return sb.toString();
    }

    private String getResultMapInfo(List<Field> fields) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        for (Field field : fields) {
            if (i == 0) {
                sb.append(String.format("<id column=\"%s\" property=\"%s\" jdbcType=\"%s\" />", field.getTableField(), field.getFieldName(), field.getDataType()));
            } else {
                sb.append(String.format("<result column=\"%s\" property=\"%s\" jdbcType=\"%s\" />", field.getTableField(), field.getFieldName(), field.getDataType()));
            }
            i++;
            sb.append("\n");
        }

        return sb.toString();

    }

}
